package org.example.springioc;

import org.example.springioc.Controller.UserController;
import org.example.springioc.Controller.UserController2;
import org.example.springioc.Controller.UserController3;
import org.example.springioc.model.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringIocApplication {

    public static void main(String[] args) {
//        //获取 Spring 上下文对象
//        ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
//        //从 Spring 上下文中获取对象
//        UserService userService = context.getBean(UserService.class);
//
//        userService.hello();
//    }

//        /*
//        这里这个 context 可以直接理解为容器
//         */
//        //使用 @Bean 时
//        //获取 Spring 上下文对象
//        ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
//        //从 Spring 上下文中获取对象
//        User user = context.getBean(User.class);
//        //使用对象
//        System.out.println(user);

        //当定义多个对象的时候, 只能根据名称来获取对象
//        ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
//        User user1 = context.getBean("u1", User.class);
//        System.out.println(user1);
//        System.out.println();
//        User user2 = context.getBean("user2", User.class);
//        System.out.println(user2);

        //获取 Controller 中的方法
//        ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
//        UserController userController = (UserController)context.getBean("userController");
//        userController.hello();

//        ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
//        UserController2 userController2 = (UserController2)context.getBean("userController2");
//        userController2.hello();
//
//        ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
//        User user = context.getBean("u1",User.class);
//        System.out.println(user);

        //@Resource
        ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
        User userController3 =  context.getBean("user2",User.class);
        System.out.println(userController3);
    }
}
